{include file="public:header"}
<!--aside nav-->
{include file="public:left"}
<!--tabStyle-->
<script type="text/javascript" src="__STATIC__/index/js/laydate/laydate.js"></script>
<script>
$(document).ready(function(){
    laydate({
      elem: '#time', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎，因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'
      format: 'YYYY-MM-DD hh:mm:ss',//YYYY/MM/DD hh:mm:ss
      istime: true,
      event: 'focus' //响应事件。如果没有传入event，则按照默认的click
    });
});
</script>
<div class="content-box role" style="margin-top: 20px">
  <div class="content-box-content">
    <div class="tab-content default-tab" id="form" style="display: block;">
      <form method="post" action="submitsms.aspx" name="clientForm" id="clientForm">
        <table cellspacing="0" cellpadding="0" width="100%" border="0">
          <tbody>
            <tr class="alt-row">
              <td width="10%" align="right" valign="top" style="line-height:30px;">
                <div style="line-height:30px;margin-bottom:60px;">短信内容：</div></td>
              <td style="line-height:40px;padding-top:6px;" width="400" align="left">
                <textarea name="msgcontent" style="border:1px solid #6E9FDE;width:380px;height:85px;" id="content" onkeypress="checkContentLength('content',1000,'message')" rows="5" onkeyup="keydown();"></textarea>
                <div style="line-height:20px;clear:both;margin-top:5px;width:500px;">
                  <span id="message">已输入
                    <font color="red">0</font>个字，还剩
                    <font color="red">1000</font>字，最多
                    <font color="red">1000</font>个字，手机
                    <font color="red">1</font>条,小灵通
                    <font color="red">1</font>条</span>
                  </div>
                <div style="width:380px;clear:both;">
                  <!-- <input name="btnAdd1" id="check" class="button" onclick="makeRequest('checkkeyword.aspx')" type="button" value="检查屏蔽词"> -->
                  <input name="btnAdd2" id="common" class="button" onclick="openSms();" type="button" value="常用短信">
                  <input name="btnAddSms" id="btnAddSms" class="button" onclick="makeRequest()" type="button" value="保存草稿">
                </div>
              </td>
              <td rowspan="10" valign="top" width="390" style="border-left:#E6E6E6 solid 1px;line-height:25px;color:#FF6600; padding-left:8px;padding-top:0px;">
                <strong>注意：</strong>
                <br>1、单次提交最好不超过20万个号码。
                <br>2、手动输入号码请用英文逗号分隔。
                <br>3、号码文件只支持TXT、EXCEL文件格式。
                <br>4、请使用CTRL+V粘贴短信内容。
                <br>5、汉字、数字、英文和标点符号都表示1个长度。
                <br>6、发手机一条为70个字。长短信为每条67个字。
                <br>7、小灵通一条为60个字，长短信在每60个字的基础上每加54个字算加一条。
                <br><br><strong>计费方式：</strong>
                {volist name="billing_list" id="vo"}
                <br>少于{$vo.number}条,收费{$vo.price}元
                {/volist}
                <br>总数超过最高条数，按最少费用来计费
                <br>
              </td>
              </tr>
            <tr>
              <td height="34" width="10%" style="line-height:34px;" align="right">预定发送时间：</td>
              <td height="34" style="height:34px;line-height:34px;" align="left">
                <span>
                  <input style="width:135px" id="time" name="time" value="" type="text" class="Wdate">
                </span>
                <span style="line-height:34px;">若为空，表示立即发送。</span>
              </td>
            </tr>
            <tr class="alt-row">
              <td height="28" style="line-height:28px;" width="10%" align="right">手机号码：</td>
              <td valign="top" style="padding-top:8px;">
                <div id="mobilelibPanel" style="border:1px solid #6E9FDE;width:380px; margin-bottom:5px; display:none;"></div>
                <textarea name="mobile" style="border:1px solid #6E9FDE;width:380px; height:85px; margin-bottom:5px;" id="mobile" rows="6" onblur="CountNum();" onchange="CountNum();"></textarea>
                <div id="mobiletip" style="color:red;margin-left:7px;line-height:24px;height:24px;margin-top:0;margin-bottom:0;">共计号码：<span id="mobile_list">0</span> 个&nbsp;&nbsp;费用：<span id="mobile_total_price">0</span>元</div>
                <div style="margin-top:0;margin-bottom:0;">
                  <input name="impmobilelib" id="impmobilelib" class="button" style="margin-bottom:3px;" onclick="mobilelib();" type="button" value="号码库导入">
                  <input name="imptxt" id="imptxt" class="button" style="margin-bottom:3px;" onclick="send();" type="button" value="TXT导入">
                  <input name="impexcel" id="impexcel" class="button" style="margin-bottom:3px;margin-left:5px;" onclick="sendexcel()" type="button" value="EXCEL导入">
                  <input name="impclient" id="impclient" class="button" style="margin-bottom:3px;margin-left:5px;" onclick="sendClient()" type="button" value="通讯录">
                  <input name="checkmobile" id="checkmobile" class="button" style="margin-bottom:3px;margin-left:5px;" onclick="clearErro()" type="button" title="过滤错误号码" value="过滤错号">
                  <input name="checkmobile2" id="checkmobile2" class="button" style="margin-bottom:3px;margin-left:5px;" onclick="repeat()" type="button" title="过滤重复号码" value="重号过滤">
                </div>
              </td>
            </tr>
            <tr>
              <td height="25" width="10%" align="right"></td>
              <td height="25" align="left">
                <div align="left" style="padding-bottom:4px;padding-top:4px;">
                  <input type="button" class="link_btn send_sms" onclick="send_sms()" value="发送短信">
                  <input type="hidden" name="hetong" id="hetong" value="0">
                  <!-- <input style="margin-left:10px;" class="link_btn" name="btnReset" id="btnReset" type="reset" value="  清除信息  "> -->
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </form>
    </div>
  </div>
</div>
<script> 
    var msgLength = 1000;
    function openSms(){
      var openSms_url = "{:url('Sms/is_ajax_userd')}";

      //页面层
      layer.open({
        type: 2,
        skin: 'layui-layer-rim', //加上边框
        area: ['90%', '90%'], //宽高
        scrollbar: true,
        //content: "<iframe src='"+openSms_url+"' style='width:100%;height:100%'><iframe>"
        content:openSms_url
      });
    }
    function makeRequest(){
      var url = "{:url('Sms/is_ajax_userd')}";
      var content = $("#content").val();
      
      if(content == ""){
        alert('请填写短信内容!');
        return false;
      }
      $.post(url,{content:content},function(result){
          alert('添加成功!');
      });
      return false;
    }
    //txt导入
    function send() {
        var url="{:url('Sms/txt_add')}";

        //页面层
        layer.open({
          type: 2,
          title:'上传txt文件',
          skin: 'layui-layer-rim', //加上边框
          area: ['200px', '200px'], //宽高
          content: url
        });
    }
    //sendexcel
    function sendexcel() {
        var url="{:url('Sms/exel_add')}";

        //页面层
        layer.open({
          type: 2,
          title:'上传exel文件',
          skin: 'layui-layer-rim', //加上边框
          area: ['200px', '200px'], //宽高
          content: url
        });
    }
    //号码库导入
    function mobilelib() {
      var url = "{:url('Sms/mobile_lib_add')}";
      var mobile_content = $("#mobile").val();
      var index = layer.load(2, {shade: false}); //0代表加载的风格，支持0-2
      $.post(url,{},function(result){
          var msg = result.msg;
          if(mobile_content != ""){
            msg = ','+msg;
            mobile_content+=msg;
          }else{
            mobile_content = msg;
          }
          $("#mobile").val(mobile_content);
          repeat();
          clearErro();
          layer.close(index);
      });
    }
    //通讯录
    function sendClient() {
      var url = "{:url('Sms/address_book_add')}";
      var mobile_content = $("#mobile").val();
      //加载层
      var index = layer.load(2, {shade: false}); //0代表加载的风格，支持0-2
      $.post(url,{},function(result){
          var msg = result.msg;
          if(mobile_content != ""){
            msg = ','+msg;
            mobile_content+=msg;
          }else{
            mobile_content = msg;
          }
          $("#mobile").val(mobile_content);
          repeat();
          clearErro();
          layer.close(index);
      });
    }
    function DataLength(fData) {
        var intLength = 0
        for (var i = 0; i < fData.length; i++) {
            if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255))
                intLength = intLength + 2
            else
                intLength = intLength + 1
        }
        return intLength
    }
    var smslong=70;
    if(smslong<=0){
        smslong=70;
    }
    var longsms_long = 67;//长短信长度
    var reduceNum = 1;
    function checkContentLength() {
        var content = document.getElementById(arguments[0]);
        var length = msgLength;

        if (arguments.length == 3) {
            //length = arguments[1];
            length = msgLength;
            var message = document.getElementById(arguments[2]);

            var l=content.value.length;

            if($("#useUserSign").attr("checked")==true)
            {
                if($("#signID option:selected").val()!="999")
                {
                    l=l+$("#signID option:selected").text().length;
                    useSign = true;
                }
            }

            var b = msgLength - l;
            if (l < 0) {
                l = 0;
            }
            if (b < 0) {
                b = 0;
            }

          var tellength=telcount(l);
          var phonelength=mobilecount(l,smslong);
          var firstMsg = "";
  
            message.innerHTML = firstMsg+"已输入<font color='red'>" + l + "</font>个字，还剩<font color='red'>" + b + "</font>字，最多<font color='red'>"+msgLength+"</font>个字，手机<font color='red'>"+phonelength+"</font>条,小灵通<font color='red'>"+tellength+"</font>条";
        } else if (arguments.length == 2) {
            //length = arguments[1];
            length = msgLength;
        }
        if (l > length) {
            alert("您输入的文字长度达到最多"+msgLength+"个字，请酌情删减！");
        }
    }
    function telcount(contentLength)
    {
        
        if($("#useUserSign").attr("checked")==true)
        {
            if($("#signID option:selected").val()!="999")
            {
                contentLength = contentLength +$("#signID option:selected").text().length;
            }
        }
        var totalcount = 1;
        if (contentLength > 60 && contentLength <= (60 + 54))
        {
            totalcount = 2;
        }
        else if (contentLength > (60 + 54) && contentLength <= (60 + 54 * 2))
        {
            totalcount = 3;
        }
        else if (contentLength > (60 + 54 * 2) && contentLength <= (60 + 54 * 3))
        {
            totalcount = 4;
        }
        else if (contentLength > (60 + 54 * 3))
        {
            totalcount = 5;
        }
        return totalcount;
    }
    function mobilecount(contentLength, firstLength)
    {
        var totalcount = 1;
        if(contentLength <= firstLength)
        {
            totalcount = 1;
        }else{
            totalcount = Math.ceil(contentLength/longsms_long);
        }
        reduceNum = totalcount;
        return totalcount;
    }
    function keydown() {
        var content = document.getElementById("content");
        var length = msgLength;
        var message = document.getElementById("message");
        

        var l=content.value.length;
        
        var useSign = false;
        
        if($("#useUserSign").attr("checked")==true)
        {
            if($("#signID option:selected").val()!="999")
            {
                l=l+$("#signID option:selected").text().length;
                useSign = true;
            }
        }

        var b = msgLength - l;

        if (((event.ctrlKey) && (event.keyCode == 86 || event.keyCode == 88)) || event.keyCode == 8 || event.keyCode == 32 || event.keyCode == 46|| event.keyCode == 13|| (event.keyCode >=48&&event.keyCode <=90)) {              //屏蔽 Ctrl+n
            if (l < 0) {
                l = 0;
            }
            if (b < 0) {
                b = 0;
            }
            var tellength=telcount(l);
            var phonelength=mobilecount(l,smslong);

            var firstMsg = "";
            if(useSign)
            {
                firstMsg = "包含签名";
            }

            message.innerHTML = firstMsg+"已输入<font color='red'>" + l + "</font>个字，还剩<font color='red'>" + b + "</font>字，最多<font color='red'>"+msgLength+"</font>个字，手机<font color='red'>"+phonelength+"</font>条,小灵通<font color='red'>"+tellength+"</font>条";
            if (l > length) {
                alert("您输入的文字长度达到最多"+msgLength+"个字，请酌情删减！");
            }
        }
    }
    function clearErro() {
      var textarea = document.getElementById("mobile");
      var patrn = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、]/ig; 
      var tem = textarea.value.replace(patrn,",");
      var temp = tem.split(/[\n ,]/g);
      
      var temps = [];
      var r = 0;
      for(var i = 0; i<temp.length; i++){
        temp[i] = temp[i].replace(/\s+/g,"");
        
        if(/^1(3|4|5|6|7|8|9)\d{9}$/.test(temp[i]) && temps.indexOf(temp[i]) == -1){ 
          temps.push(temp[i]);
          r++;
        } 
      }
      
      textarea.value = temps.join(",");
      $("#mobile_list").text(r);

      //计算号码费用
      mobile_cost(r);
    }
    function repeat(){
      var textarea = document.getElementById("mobile");
      if(textarea.value == ""){
        return;
      }
    
      var temp = textarea.value.split(/[\n ,]/g);
      var temps = [];
      var r = 0;
      for(var i = 0; i<temp.length; i++){
        if(temps.indexOf(temp[i]) == -1){
          temps.push(temp[i]);
          r++;
        }
      }
      textarea.value = temps.join(",");
      $("#mobile_list").text(r);
    }
    function CountNum(){
      repeat();
      clearErro();
    }
    function mobile_cost(num){
      var url = "{:url('Sms/calculation')}"
      $.post(url,{mobile_count:num},function(result){
          var m = result.msg * reduceNum;
          $("#mobile_total_price").text(m.toFixed(2));
      });
    }
    function send_sms(){
      var formData = new FormData();
      var content = $("#content").val();
      var time = $("#time").val();
      var mobile = $('#mobile').val();
      if(content == ""){
        alert('短信内容为空!');
        return;
      }
      if(mobile == ""){
        alert('电话为空!');
        return;
      } 
      var hetong = $("#hetong").val();
      if(hetong == '0'){
        var url = "{:url('Member/contract_display_sms',['id'=>'4'])}";

        //页面层
        var index = layer.open({
          type: 2,
          title:'合同信息',
          skin: 'layui-layer-rim', //加上边框
          area: ['700px', '700px'], //宽高
          content:url 
        });
        return false;
      }
      formData.append('content',content);
      formData.append('send_time',time);
      formData.append('mobile_string',mobile);

      $.ajax({
          url:"{:url('Sms/send_sms')}",
          type:'POST',
          data:formData,
          async:false,
          processData:false,
          contentType:false,
          success:function(result) {
              layer.close(index);
              if(result.code == '1'){
                if(time == ''){
                  alert('发送成功!');
                }else{
                  alert('写入成功,将在'+time+'发送!');
                }
              }else{
                alert(result.msg);
              }
              window.location.reload();
          }
      });
    }
    function trim(str) { 
      return str.replace(/^\s+|\s+$/g, ''); 
    }
</script>
</body>
</html>
